package openadmin.model.person;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import openadmin.dao.Base;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;

@Entity
@Table(name = "person", schema = "person", uniqueConstraints = {@UniqueConstraint(columnNames="nif"), @UniqueConstraint(columnNames="description")})
public class Person implements Base, Cloneable, java.io.Serializable{


	/** attribute that contains the identifier*/
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)	
	private Long id;
	
	/** attribute that contains the description, unique value*/
	@Length(max = 60)
	@NotNull
	private String description;

	@Length(max = 20)
	private String name;

	@Length(max = 6)
	private String partsurname1;
	
	@Length(max = 20)
	private String surname1;

	@Length(max = 6)
	private String partsurname2;
	
	@Length(max = 20)
	private String surname2;

	@Length(max = 50)
	@NotNull
	private String fullname;

	@Length(max = 9)
	@NotNull
	private String nif;

	@NotNull
	private char state;

	private String note;
	
	/** Transient attribute that means that the system should make a log on any JPA operation of this class*/
	@Transient
	boolean debuglog = true;
	
	/** Transient attribute that means that the system should make a historic on any JPA operation of this class*/
	@Transient
	boolean historiclog = true;

	public Person() {
		super();
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String pDescripcion) {
		
		//for search
		if (this.getNif() == null || this.getFullname() == null){
			
			this.description = pDescripcion;
			
		} else {
		
			this.description = this.getNif() + " " + this.getFullname().toUpperCase().replaceAll(" ", "").trim();
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name.toUpperCase();
		updateFullName();
	}

	public String getPartsurname1() {
		return partsurname1;
	}

	public void setPartsurname1(String partsurname1) {
		this.partsurname1 = partsurname1.toUpperCase();
		updateFullName();
	}

	public String getSurname1() {
		return surname1;
	}

	public void setSurname1(String surname1) {
		this.surname1 = surname1.toUpperCase();
		updateFullName();
	}

	public String getPartsurname2() {
		return partsurname2;
	}

	public void setPartsurname2(String partsurname2) {
		this.partsurname2 = partsurname2.toUpperCase();
		updateFullName();
	}

	public String getSurname2() {
		return surname2;
	}

	public void setSurname2(String surname2) {
		this.surname2 = surname2.toUpperCase();
		updateFullName();
	}

	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname.toUpperCase();
		setDescription("");
	}

	public String getNif() {
		return nif;
	}

	public void setNif(String nif) {
		
		String s = "000000000" + nif;
		String ss = s.substring(s.length()-9, s.length());		
		this.nif = ss;
		setDescription("");
	}

	public char getState() {
		return state;
	}

	public void setState(char state) {
		this.state = state;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note.toUpperCase();
	}
	
	public boolean getDebugLog() {
		
		return debuglog;
	}

	public void setDebuglog(boolean debuglog) {
		this.debuglog = debuglog;
	}

	public boolean getHistoricLog() {
		return historiclog;
	}

	public void setHistoricLog(boolean historiclog) {
		this.historiclog = historiclog;
	}
	
	private void updateFullName(){
		
		fullname = (partsurname1 + " " + surname1 + " " + partsurname2 + " " + surname2 + " " + name)
		.replace("null", "").replace("  ", " ").trim();
		setDescription("");
	}

	@Override
	public String toString(){
		 
		 return nif + " " + fullname;
	}
	
}
